//
//  Location.swift
//  BellManage
//
//  Created by Bestsch on 16/3/30.
//  Copyright © 2016年 wsl. All rights reserved.
//

import Foundation

/**
 获取文件路径
 
 - returns: 文件路径
 */
func documentsPath()-> String{
    let paths: NSArray = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
    let documentDirectory = paths.object(at: 0)
    let plistPath = (documentDirectory as! NSString).appendingPathComponent("Location.plist")
    return plistPath
}
/**
 *  初始化
 *
 *  @return 初始化本地文件
 */
func initLocationData(){
    let filePath = documentsPath()
    let applist = NSMutableDictionary()
    let array: NSMutableArray = ["\(appname)","王胜龙","人员保存信息"]
    applist.setObject(array, forKey: "initInfo" as NSCopying)
    applist.write(toFile: filePath, atomically: true)
}
/**
 保存权限
 
 - parameter array: 权限数据
 */
func setRightArray(_ array: NSMutableArray) {
    let filePath = documentsPath()
    let applist: NSMutableDictionary = (NSMutableDictionary.init(contentsOfFile: filePath)?.mutableCopy())! as! NSMutableDictionary
    
#if EDUMANAGE
    //在铃铛管家中增加 - 全校通知 先在接口出来之前做
//    let schnotice = ["plugimg":"schnotice","unread":"5","plugid":"","serid":"","apiurl":"","plugname":"全校通知","modetype":"16"] as Any
//    array.insert(schnotice, at: 0)
#endif
 
    
    applist.setObject(array, forKey: "RightInfo" as NSCopying)
    applist.write(toFile: filePath, atomically: true)
}
/**
 获取权限模块数组
 
 - returns: 返回的权限数组
 */
func getRightArray() -> NSMutableArray {
    let filePath = documentsPath()
    let applist: NSMutableDictionary = (NSMutableDictionary.init(contentsOfFile: filePath)?.mutableCopy())! as! NSMutableDictionary
    return applist.object(forKey: "RightInfo") as! NSMutableArray
}

//MARK: - 设置办公模块的

func setOfficeRightArray(_ array: NSMutableArray) {
    let filePath = documentsPath()
    let applist: NSMutableDictionary = (NSMutableDictionary.init(contentsOfFile: filePath)?.mutableCopy())! as! NSMutableDictionary
    
    applist.setObject(array, forKey: "OfficeRightInfo" as NSCopying)
 
    if (applist.write(toFile: filePath, atomically: true)){
    }else{
        print("失败")
    }

}

func getOfficeRightArray() -> NSMutableArray {
    let filePath = documentsPath()
    let applist: NSMutableDictionary = (NSMutableDictionary.init(contentsOfFile: filePath)?.mutableCopy())! as! NSMutableDictionary
    let array = applist.object(forKey: "OfficeRightInfo")
    if array != nil {
        return array as! NSMutableArray
    }else {
        return NSMutableArray()
    }
    
}

//存储为本地db文件

func setLocationData(_ object: String, key: String) {
    let store = YTKKeyValueStore(dbWithName: DBName)
    
    if !(store?.isTableExists(tableName))! {
        store?.createTable(withName: tableName)
    }

    store?.put(object, withId: key, intoTable: tableName)
}

func getLocationData(key: String) -> NSMutableArray {
    let store = YTKKeyValueStore(dbWithName: DBName)
    if (store?.isTableExists(tableName))! {
        
        let tempString = store?.getObjectById(key, fromTable: tableName)
        if tempString != nil {
            let tempArray = JsonFormt(str: (tempString! as! NSArray).firstObject! as! String)
            return tempArray
        }else {
            return NSMutableArray()
        }
        
        
    }else {
        return NSMutableArray()
    }
    
}


